package ${package.Controller};

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.yaya.common.aspect.crypto.Crypto;
import com.yaya.common.param.RequestParam;
import com.yaya.common.result.ResponseResult;
import ${config.packageConfig.parent}.dtomain.${table.entityName}Dto;
import ${config.packageConfig.parent}.service.${table.serviceName};

/**
 * ${table.comment}控制器
 * 
 * @author ${author}
 * @since ${date[0..10]}
 */
@RestController
<#--@RequestMapping("${schemaName?substring(0,schemaName?length-1)}<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/${controllerMappingHyphen}")-->
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/${controllerMappingHyphen}")

public class ${table.controllerName} {

  @Autowired
  private ${table.serviceName} ${table.serviceName?substring(1,table.serviceName?length)?uncap_first};

  /**
   * 新增${table.comment}
   * 
   * @${table.entityName?uncap_first}Dto ${table.comment} 传输对象
   * @return 新增结果
   */
  @Crypto
  @PostMapping("insert")
  public ResponseResult insert(@RequestBody RequestParam <${table.entityName}Dto> ${table.entityName?uncap_first}Dto) {
    ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.insert(${table.entityName?uncap_first}Dto.getData());
    return ResponseResult.success();
  }

  /**
   * 批量新增${table.comment}
   *
   * @${table.entityName?uncap_first}Dtos ${table.comment} 传输对象列表
   * @return 新增结果
   */
  @Crypto
  @PostMapping("inserts")
  public ResponseResult inserts(@RequestBody RequestParam<List<${table.entityName}Dto>> ${table.entityName?uncap_first}Dto) {
    if (${table.entityName?uncap_first}Dto.getData() == null || ${table.entityName?uncap_first}Dto.getData().size() == 0) {
      return ResponseResult.error("不能新增空白列表");
    }
    ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.inserts(${table.entityName?uncap_first}Dto.getData());
    return ResponseResult.success();
  }

  /**
   * 删除${table.comment}
   * 
   * @${table.entityName?uncap_first}Ids ${table.comment} 主键列表
   * @return 删除结果
   */
  @Crypto
  @PostMapping("delete")
  public ResponseResult delete(@RequestBody RequestParam<List<String>> ${table.entityName?uncap_first}Dto) {
    ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.delete(${table.entityName?uncap_first}Dto.getData());
    return ResponseResult.success();
  }

  /**
   * 修改${table.comment}
   * 
   * @${table.entityName?uncap_first}Dto ${table.comment} 传输对象
   * @return 修改结果
   */
  @Crypto
  @PostMapping("update")
  public ResponseResult update(@RequestBody RequestParam<${table.entityName}Dto> ${table.entityName?uncap_first}Dto) {
    ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.update(${table.entityName?uncap_first}Dto.getData());
    return ResponseResult.success();
  }

  /**
   * 查询所有${table.comment}
   * 
   * @${table.entityName?uncap_first}Dto ${table.comment} 查询对象
   * @return 查询结果
   */
  @Crypto
  @PostMapping("list")
  public ResponseResult list(@RequestBody RequestParam<${table.entityName}Dto> ${table.entityName?uncap_first}Dto) {
    List<${table.entityName}Dto> ${table.entityName?uncap_first}Dtos = ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.list(${table.entityName?uncap_first}Dto.getData());
    return ResponseResult.success().put(ResponseResult.RECORDS_TAG, ${table.entityName?uncap_first}Dtos);
  }

  /**
   * 分页查询${table.comment}
   * 
   * @${table.entityName?uncap_first}Dto request 请求对象
   * @${table.entityName?uncap_first}Dto ${table.comment} 查询对象
   * @return 分页结果
   */
  @Crypto
  @PostMapping("page")
  public ResponseResult page(@RequestBody RequestParam<${table.entityName}Dto> ${table.entityName?uncap_first}Dto)
      throws UnsupportedEncodingException, JsonMappingException, JsonProcessingException {
    Map<String, Object> map = ${table.serviceName?substring(1,table.serviceName?length)?uncap_first}.page(${table.entityName?uncap_first}Dto);
    ResponseResult result = ResponseResult.success();
    result.put(ResponseResult.TOTAL_TAG, map.get(ResponseResult.TOTAL_TAG));
    result.put(ResponseResult.RECORDS_TAG, map.get(ResponseResult.RECORDS_TAG));
    return result;
  }

}
